from PyQt6.QtWidgets import QLabel
from PyQt6.QtCore import Qt, QTimer, QPropertyAnimation, QPoint, QEasingCurve
from PyQt6.QtGui import QPainter, QColor, QPainterPath

class Toast(QLabel):
    """自定义Toast提示框"""
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowFlags(Qt.WindowType.FramelessWindowHint | Qt.WindowType.Tool | Qt.WindowType.WindowStaysOnTopHint)
        self.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.setStyleSheet("""
            QLabel {
                color: white;
                font-size: 15px;
                font-weight: bold;
                padding: 15px 30px;
                border-radius: 25px;
                background-color: #4CAF50;  /* 使用绿色背景 */
                border: 2px solid #45a049;
            }
        """)
        self.timer = QTimer()
        self.timer.timeout.connect(self.hide_animation)
        
    def show_message(self, text, duration=1500):
        """显示消息"""
        self.setText(text)
        self.adjustSize()
        
        # 计算位置 - 底部中间，在输入框上方
        parent = self.parent()
        if parent:
            pos = parent.mapToGlobal(parent.rect().bottomLeft())
            x = pos.x() + (parent.width() - self.width()) // 2
            y = pos.y() - self.height() - 80  # 在输入框上方80像素
            self.move(x, y)
        
        # 显示动画
        self.setWindowOpacity(0.0)
        self.show()
        
        # 淡入动画
        fade_in = QPropertyAnimation(self, b"windowOpacity", self)
        fade_in.setDuration(200)
        fade_in.setStartValue(0.0)
        fade_in.setEndValue(1.0)
        fade_in.setEasingCurve(QEasingCurve.Type.OutCubic)
        fade_in.start()
        
        # 向上滑动动画（从下往上）
        slide = QPropertyAnimation(self, b"pos", self)
        slide.setDuration(200)
        start_pos = self.pos()
        slide.setStartValue(QPoint(start_pos.x(), start_pos.y() + 20))  # 从下面开始
        slide.setEndValue(start_pos)
        slide.setEasingCurve(QEasingCurve.Type.OutCubic)
        slide.start()
        
        # 设置定时器
        self.timer.start(duration)
        
    def hide_animation(self):
        """隐藏动画"""
        self.timer.stop()
        
        # 淡出动画
        fade_out = QPropertyAnimation(self, b"windowOpacity", self)
        fade_out.setDuration(200)
        fade_out.setStartValue(1.0)
        fade_out.setEndValue(0.0)
        fade_out.setEasingCurve(QEasingCurve.Type.InCubic)
        fade_out.finished.connect(self.hide)
        
        # 向上滑动动画
        slide = QPropertyAnimation(self, b"pos", self)
        slide.setDuration(200)
        start_pos = self.pos()
        slide.setStartValue(start_pos)
        slide.setEndValue(QPoint(start_pos.x(), start_pos.y() - 20))
        slide.setEasingCurve(QEasingCurve.Type.InCubic)
        
        fade_out.start()
        slide.start() 